package com.net.xpay.web.controller.merchant;

import com.alipay.api.AlipayApiException;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.model.SignAuthModel;
import com.net.xpay.common.service.AlipayUserSignService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.web.secutiry.RequestUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@Controller
@Slf4j
@RequestMapping("/merchant/alipay-sign-auth")
@Api(tags = "商户/支付宝授权", description = "商户 支付宝授权 net")
public class AlipayAuthV2Controller {

    @Autowired
    private AlipayUserSignService alipayUserSignService;


    @ApiOperation(value = "批量有密签约授权一体化接口")
    @PostMapping("/sign-auth")
    @ResponseBody
    public ResultData<Void> createAlipaySingAuth(@ApiIgnore @RequestUser User user, @RequestBody SignAuthModel signAuthModel) {
        try {
            alipayUserSignService.signAuth(user, signAuthModel);
        } catch (AlipayApiException e) {
            log.info("createAlipaySingAuth error|params={}|userId={}", SerializationUtil.obj2String(signAuthModel), user.getId(), e);
            throw new BusinessException(PoseidonErrorCode.BUSINESS_ERROR, e.getErrMsg());
        }

        return new ResultData<>();
    }

}
